package com.cy.personer.biz.impl;

import com.cy.personer.bean.FirstMenu;
import com.cy.personer.bean.Role;
import com.cy.personer.bean.SecondMenu;
import com.cy.personer.biz.PowerBiz;
import com.cy.personer.mapper.PowerMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PowerBizImpl implements PowerBiz {
    @Resource
    private PowerMapper powerMapper;


  //角色查询
    @Override
    public List<Role> querys() {
        return powerMapper.querys();
    }


    //查找所有一级菜单
    @Override
    public List<FirstMenu> firstList() {
        return powerMapper.firstList();
    }
    //查找所有二级菜单
    @Override
    public List<SecondMenu> secondList() {
        return powerMapper.secondList();
    }
   //查找所有二级菜单的ID
    @Override
    public List<SecondMenu> queryList(int roleId) {
        return powerMapper.queryList(roleId);
    }

    @Override
    public boolean add(int roleId, Integer[] secondMenuId) {
        boolean addFlag=true;
        boolean delFlag=true;
      Map<String,Object>map=new HashMap<>();
      map.put("roleId",roleId);
      map.put("secondMenuId",secondMenuId);
      List<SecondMenu>second =powerMapper.queryList(roleId);
      if(second.size()>0){
          delFlag=powerMapper.del(roleId);
          addFlag = powerMapper.add(map);
      }else{
          addFlag = powerMapper.add(map);
      }
      if(delFlag==true&&addFlag==true){
          return  true;
      }
       return  false;
//    }
}

    @Override
    public PageInfo<Role> getRoles( Integer pageNum) {
        Page<Role> page =PageHelper.startPage(pageNum,5);
         powerMapper.querys();
        PageInfo<Role> PageInfo = new PageInfo<>(page);
        return PageInfo;
    }
//    @Override
//    public boolean add(Map<String, Object> map) {
//        int roleId = (int) map.get("roleId");
//        powerMapper.del(roleId);
//        powerMapper.add(map);
//        return true;
//    }


}
